// pages/evaluate/evaluate.js
var app = getApp();
const util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    httpApiImg: app.globalData.httpApiImg,
    userInfo: '',
    starList: [],
    shopstarList: [
      { checked: false },
      { checked: false },
      { checked: false },
      { checked: false },
      { checked: false }
    ],
    canSubmit:false,
    content:'',
    orderInfo:'',
    evaluateUrl: [], 
    formData: [],
    warelength: 0,
    imgins: 'evaluateUrl'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo')),
      orderInfo: JSON.parse(options.item)
    })
    var warelist = this.data.orderInfo.wareList;
    for (var i = 0; i < warelist.length; i++){
      var obj = {
        userId: JSON.parse(wx.getStorageSync('userInfo')).id,		//	用户id	
        evaluateNote: '', 		//	评价内容				
        evaluateUrl: [], 	//	评价图片		
        productId: warelist[i].productId,  //	商品id	
        productStar: 0, 		//	商品星星	
        shopId: options.id, 	//	商家id	
        shopStar: 0,  //	店铺星星
        backupB: options.backup   // 订单号
      }
      var arr = [
        { checked: false },
        { checked: false },
        { checked: false },
        { checked: false },
        { checked: false }
      ]
      this.setData({
        ['evaluateUrl'+ i] : []
      })
      this.data.starList.push(arr);
      this.data.formData.push(obj);
    }
    this.setData({
      starList: this.data.starList,
      formData: this.data.formData,
      warelength: this.data.starList.length
    })
  },
  // 发布评论
  submit: function (e) {
    if (this.data.canSubmit){
      var _this = this;
      var item = _this.data.formData;
      for(var i = 0; i < item.length; i++){
        if (item[i].evaluateUrl){
          item[i].evaluateUrl = item[i].evaluateUrl.join(",");
        }
        wx.request({
          url: app.globalData.httpApiCar + 'thingEvaluate/save',
          data: item[i],
          method: 'POST',
          header: {
            'content-type': 'application/x-www-form-urlencoded' // 默认值
          },
          success: function (res) {
            if (res.data.success && (_this.data.warelength <= (i + 1))) {
              wx.redirectTo({
                url: '/pages/myOrder/myOrder?status=5&orderId=' + _this.data.orderId,
              })
            }
          }
        })
      }
    }
    
  },
  starTap:function(e){
    var id = e.currentTarget.dataset.id;
    var key = e.currentTarget.dataset.key;
    var list = e.currentTarget.dataset.list;
    var oIndex = e.currentTarget.dataset.index;
    var parentindex = e.currentTarget.dataset.parentindex;
    this.data[list][parentindex].forEach(function(ele,index){
      if (index <= oIndex){
        ele.checked=true;
      }else{
        ele.checked = false;
      }
    })
    this.setData({
      ['formData['+parentindex+'].'+key]: oIndex + 1,
      [list]: this.data[list]
    })
    if(id){
      this.setData({
        ['formData[' + parentindex+'].productId']: id
      })
    }
  },
  starShopTap: function (e) {
    var id = e.currentTarget.dataset.id;
    var key = e.currentTarget.dataset.key;
    var list = e.currentTarget.dataset.list;
    var oIndex = e.currentTarget.dataset.index; this.data[list]
    this.data[list].forEach(function (ele, index) {
      if (index <= oIndex) {
        ele.checked = true;
      } else {
        ele.checked = false;
      }
    })
    this.setData({
      [list]: this.data[list]
    })
    for (var i = 0; i < this.data.warelength; i++){
      this.setData({
        ['formData[' + i + '].' + key]: oIndex + 1
      })
    }
    if (this.data.formData[0].shopStar) {
      this.setData({
        canSubmit: true
      })
    }
  },
  inputFunc: function(e){
    var parentindex = e.currentTarget.dataset.parentindex;
    this.setData({
      ['formData[' + parentindex+'].evaluateNote']: e.detail.value,
      
    })
  },
  // 添加图片
  addPhoto: function (e) {
    var that = this,
      count = 0,
      key = e.currentTarget.dataset.id,
      parentindex = e.currentTarget.dataset.parentindex,
      only = e.currentTarget.dataset.only;
    only ? count = 1 : count = 9 - this.data[key].length;
    wx.chooseImage({
      count: count,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var imgsrc = res.tempFilePaths;
        that.uploadImg(imgsrc, key, 0, null, parentindex)
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  /*上传图片*/
  uploadImg: function (path, key, tag, oIndex, parentindex) {
    var data1 = app.uploadimg({
      url: app.globalData.httpApiCar + 'uploadfile/upload1',
      path: path,
      globalkey: this,
      realpath: this.data.formData[parentindex][key],
      showkey: 'formData[' + parentindex + '].' + key,
      key: key,
      tag: tag,
      oIndex: oIndex
    });
  },
  changeValue: function(){

  },
  // 修改图片
  changePhoto: function (e) {
    var that = this,
      key = e.currentTarget.dataset.id,
      parentindex = e.currentTarget.dataset.parentindex,
      index = e.currentTarget.dataset.value;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        var imgsrc = res.tempFilePaths;
        that.uploadImg(imgsrc, key, 1, index, parentindex)
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  delImg: function (e) {
    var key = e.currentTarget.dataset.id,
      parentindex = e.currentTarget.dataset.parentindex,
      index = e.currentTarget.dataset.value;
    this.data.formData[parentindex][key].splice(index, 1);
    this.setData({
      formData: this.data.formData
    });

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})